// #include<iostream>
// #include<unistd.h>
// #include<thread>

#pragma once
#include <iostream>
#include <pthread.h>
#include <string>
#include <ctime>

//像是把 Routine 给隐藏了  只关注 函数指针对应的函数方法
typedef void (*callback_t)();

static int num = 1;

class Thread
{
public:
    static void* Routine(void* args)
    {
        Thread*thread = static_cast<Thread* >(args);
        thread->Entery();
    }
public:
    Thread(callback_t cb) : tid_(0), name_(""), start_timestamp_(0), isrunning_(false), cb_(cb)
    {
    }

    void Run()
    {
        name_ = "thread-" + std::to_string(num++);
        start_timestamp_ = time(nullptr);
        isrunning_ = true;
        pthread_create(&tid_, nullptr, Routine, this);
    }

    void Join()
    {
        pthread_join(tid_, nullptr);
        isrunning_ = false;
    }

    std::string Name()
    {
        return name_;
    }

    uint16_t StartTimeStamp()
    {
        return start_timestamp_;
    }

    bool IsRunning()
    {
        return isrunning_;
    }

    //入口方法 回调
    void Entery()
    {
        cb_();
    }

    ~Thread()
    {
    }

private:
    pthread_t tid_;
    std::string name_;
    uint16_t start_timestamp_; // 时间戳
    bool isrunning_;

    callback_t cb_;
};

